* **************************************************************************** *
* Sierra Leone - Social Incentives for Childhood Immunization                  *
* Extensive Margin Effect of Bracelets: Vaccine Completion by 12, 18, 24 Months*
* **************************************************************************** *
/*
** Purpose:    Extensive Margin Effect of Bracelets: Complete Vaccination
				by 12, 18 and 24 months
*/
* **************************************************************************** *


* **************************************************************************** *
* Generate main outcomes - Vaccine by 12, 14, and 16 Months
* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if age_pikin >= 730   // 24 Months

	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)


	* ------------------------------------------------------------------------------
	* Generate Ouctome Variables and Controls:

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	* 12, 18, 24 months
	* 365, 548, 730 days

	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
	foreach age of numlist 365, 548, 730 {
	gen    `vaccine'_`age' = `vaccine'
	}
	}

	* Code Vaccine outcomes by 12 months, 14, and 16 months:
	foreach  var in bcg penta1 penta2 penta3 measles1 {
	foreach age of numlist 365, 548, 730 {
	replace `var'_`age' = 0          if `var'==1 & age_`var'>=`age' & age_`var'!=.
	replace `var'_`age' = 0          if `var'==. & age_pikin>=`age' & age_pikin!=.
	replace `var'_`age' = .          if age_pikin<`age'
	}
	}

	* Generate the cumulative number of vaccines:
	foreach age of numlist 365, 548, 730 {
	local   VaccineVars_`age' = " bcg_`age' penta1_`age' penta2_`age' penta3_`age' measles1_`age' "
	egen    vaccines_`age'    = rowtotal(`VaccineVars_`age'')
	}


	foreach vaccine of numlist 1(1)5 {
	foreach age of numlist 365, 548, 730 {
	gen     vaccine`vaccine'_`age' = 0          if age_pikin >= `age'
	}
	}

	foreach vaccine of numlist 1(1)5 {
	foreach age of numlist 365, 548, 730 {
	replace vaccine`vaccine'_`age' = 1          if vaccines_`age' >= `vaccine' & age_pikin >= `age'
	}
	}

	summ    vaccines_365  vaccines_548 vaccines_730

	* Code the outcome variable: number of vaccines
	gen     num_vaccine_365 = vaccines_365
	gen     num_vaccine_548 = vaccines_548
	gen     num_vaccine_730 = vaccines_730


	* --------------------------------------------------------------------------
	* Demean controls:

	tab     arm_anc, gen(anc)

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)

	summ    recall1 recall2 recall3


	foreach var in  recall1 recall2 recall3 distance clinic_pop anc2 anc3 anc4  {
	sum    `var' if intervention_arm==1, detail
	gen    `var'_dm    = `var' - `r(mean)'
	}

	* --------------------------------------------------------------------------

	* Svae the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_ExtensiveMargin_analysis.dta", replace


	* **************************************************************************** *
	* 3.) Append data sets and prepare Panel
	* **************************************************************************** *


	use "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_ExtensiveMargin_analysis.dta", clear


	label var treat3       "Signal at 4"
	label var treat4       "Signal at 5"
	label var treat2       "Uninformative Bracelet"

	local ANC_dm      = " anc2_dm anc3_dm anc4_dm  "
	local Controls_dm = " recall1_dm distance_dm clinic_pop_dm  "


	** Effects of Signals on Vaccination by 12, 18, and 24 Months:
	eststo clear
	foreach age of numlist 365, 548, 730 {
	foreach vaccine in vaccine4 vaccine5  {
	areg   `vaccine'_`age' treat3 treat4 treat2    `Controls_dm'  `ANC_dm', absorb(strata) vce(bootstrap,  reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   treat2 = treat4
	estadd scalar treat2_treat4 = r(p)
	test   treat2 = treat3
	estadd scalar treat2_treat3 = r(p)
	test   treat3 = treat4
	estadd scalar treat3_treat4 = r(p)
	test   treat2 treat3 treat4
	estadd scalar bracelets     = r(p)
	estadd scalar C_mean  = _b[_cons]
	estadd local Obs      = "`e(N)'"
	estadd local controls = "Yes"
	eststo m_`vaccine'_`age'
	}
	}

	esttab  ///
	m_vaccine4_365 m_vaccine4_548 m_vaccine4_730   ///
	m_vaccine5_365 m_vaccine5_548 m_vaccine5_730   ///
	using "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex",  ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule \\"                     ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{3}{c}{\textbf{4 Vaccines}}                                                                   & \multicolumn{3}{c}{\textbf{5 Vaccines}}  \\"     ///
	"                                                 & \multicolumn{1}{c}{by 12 months}   & \multicolumn{1}{c}{by 18 months}  & \multicolumn{1}{c}{by 24 months} & \multicolumn{1}{c}{by 12 months} & \multicolumn{1}{c}{by 18 months}  & \multicolumn{1}{c}{by 24 months}   \\")   ///
	keep(treat3 treat4 treat2)                                          ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) se(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace


	filefilter "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex"      ///
	"${Replicate_SocialSignals_Tables}/Table_ExtensiveMargin_Vaccine4_Vaccine5_withControls.tex",  from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex"



	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls

	esttab  ///
	m_vaccine4_365 m_vaccine4_548 m_vaccine4_730   ///
	m_vaccine5_365 m_vaccine5_548 m_vaccine5_730   ///
	using "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex",  ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule \\"                     ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{3}{c}{\textbf{4 Vaccines}}                                                                   & \multicolumn{3}{c}{\textbf{5 Vaccines}}  \\"     ///
	"                                                 & \multicolumn{1}{c}{by 12 months}   & \multicolumn{1}{c}{by 18 months}  & \multicolumn{1}{c}{by 24 months} & \multicolumn{1}{c}{by 12 months} & \multicolumn{1}{c}{by 18 months}  & \multicolumn{1}{c}{by 24 months}   \\")   ///
	keep(treat3 treat4 treat2)                                          ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) ${StarsOpt}   nomtitles                                       ///
	addnotes("") label b(3) p(3)  nobaselevels noconstant noobs          ///
	nolines posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex  ///
	replace


	filefilter "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex"      ///
	"${Replicate_SocialSignals_Tables}/pvalues/Table_ExtensiveMargin_Vaccine4_Vaccine5_withControls_pvals.tex",  from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_Extensive_new.tex"




* **************************************************************************** *
* End of the Dofile !!!
* **************************************************************************** *
